<?php
namespace App\Http\Controllers\ApiXcx;

use App\Service\ApiXcx\PointsOrderService;
use Illuminate\Http\Request;

class PointsOrderController extends BaseController
{
    //商品

    public function __construct()
    {
        $this->db = new PointsOrderService();
    }

    //列表
    public function getList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '该用户不存在');
        }
        $status = $request->post('status', 0);
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 5);
        $res = $this->db->getList($uid, $pages, $status);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //下单前选项
    public function getOption(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $res = $this->db->getOption();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //下单选项
    public function orderOption(Request $request)
    {
        $sku_id = $request->post('sku_id', 0);
        $shop_id = $request->post('shop_id', 0);
        $delivery_id = $request->post('delivery_id', 0);
        $num = $request->post('num', 0);
        if (!$sku_id || !$shop_id || !$delivery_id || !$num) {
            return $this->toJson(-2, '参数都是必须的');
        }
        $res = $this->db->orderOption($sku_id, $shop_id, $delivery_id, $num);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //直接下单
    public function addByGid(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $fieldMsgs = [
            'address_id' => ['en'=>'address_id','cn'=>'收货地址','ismust'=>1],
            'sku_id' => ['en'=>'sku_id','cn'=>'规格ID','ismust'=>1],
            'num' => ['en'=>'num','cn'=>'商品数量','ismust'=>1],
            'delivery' => ['en'=>'delivery','cn'=>'配送方式','ismust'=>1],
        ];
        $validate = $this->checkField($request, $fieldMsgs);
        if ($validate['code'] < 0) {
            return $this->toJson(-2, $validate['msg']);
        }
        $pas = $validate['data'];
        if (empty($pas['shop_id'])) {
            $pas['shop_id'] = $request->post('shop_id', 0);
        }
        if ($pas['delivery'] == 2 && !$pas['shop_id']) {
            return $this->toJson(-2, '门店必须的');
        }
        $pas['remark'] = $request->post('remark', '');
        $pas['uid'] = $uid;
        $res = $this->db->addByGid($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //订单取消
    public function cancel(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '该用户不存在');
        }
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '订单ID必须的');
        }
        $res = $this->db->setCancel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //订单撤回
    public function revoke(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '订单ID必须的');
        }
        $res = $this->db->setRevoke($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //再来一单
    public function again(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '订单ID必须的');
        }
        $res = $this->db->again($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //详情
    public function getOne(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '订单ID必须的');
        }
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

//    //状态发货
//    public function send(Request $request)
//    {
//        $token = $request->post('token', '');
//        $this->db->addApiCount($token);
//        $id = $request->post('id', 0);
//        if (!$id) {
//            return $this->toJson(-2, '订单ID必须的');
//        }
//        $res = $this->db->sendOut($id);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
//        return $this->toJson();
//    }

    //状态收货
    public function receive(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '订单ID必须的');
        }
        $res = $this->db->receive($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //状态自提
    public function pickup(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '订单ID必须的');
        }
        $res = $this->db->pickup($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}